package com.ahdy.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.time.LocalTime;

// 8. 用药记录实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "medication_records")
public class MedicationRecord {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @NotNull(message = "任务ID不能为空")
    @Column(nullable = false)
    private Integer taskId;

    @NotNull(message = "老人ID不能为空")
    @Column(nullable = false)
    private Integer userId;

    @NotNull(message = "药物ID不能为空")
    @Column(nullable = false)
    private Integer medicationId;

    @NotNull(message = "计划服药时间不能为空")
    @JsonFormat(pattern = "HH:mm:ss")
    @Column(nullable = false)
    private LocalTime plannedTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime actualTime;

    @Size(max = 20, message = "实际服用剂量长度不能超过20字符")
    @Column(length = 20)
    private String dosageTaken;

    @NotNull(message = "服药状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private RecordStatus status;

    @Min(value = 0, message = "延迟分钟数不能为负数")
    @Column(columnDefinition = "INT DEFAULT 0")
    private Integer delayMinutes = 0;

    @Column(columnDefinition = "TEXT")
    private String notes;

    @Enumerated(EnumType.STRING)
    @Column(columnDefinition = "ENUM('本人','家属','护理员') DEFAULT '本人'")
    private RecordedBy recordedBy = RecordedBy.本人;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(updatable = false)
    private LocalDateTime createdAt;

    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
    }

    public enum RecordStatus {
        按时服用, 延迟服用, 跳过, 忘记服用
    }

    public enum RecordedBy {
        本人, 家属, 护理员
    }
}
